"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 主机间ssh失败执行pscp命令
Create At   : 2022/4/24
Owner       : opentestcase023
Description :
    1.创建传输源文件
    2.清除待操作主机间root互信
    3.执行pscp命令
    4.清理环境
Expect      :
    1.创建成功
    2.清理成功
    3.执行失败
    4.成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 > primary_sh.get_node_num(), '非1+2不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_root_node = Node('PrimaryRoot')
        self.s_root_node1 = Node('Standby1Root')
        self.s_root_node2 = Node('Standby1Root')
        self.com_root = CommonSH("PrimaryRoot")
        self.common = Common()
        self.ssh_file = os.path.join('~', '.ssh', '*')
        self.dir_path = os.path.dirname(macro.DB_INSTANCE_PATH)

    def test_pscp(self):
        text = '-----step1:构建传输文件;expect:成功-----'
        self.log.info(text)
        global file_path
        file_path = os.path.join(f'{self.dir_path}', 'backup.txt')
        add_cmd = f'touch {file_path};ls {self.dir_path}'
        self.log.info(add_cmd)
        add_res = self.common.get_sh_result(self.primary_root_node, add_cmd)
        self.log.info(add_res)
        self.assertIn('backup.txt', add_res, '文件构建失败')

        text = '-----step2:清除待操作主机间root互信;expect:成功'
        self.log.info(text)
        del_cmd = f'rm -rf {self.ssh_file}'
        self.log.info(del_cmd)
        del_res = self.common.get_sh_result(self.primary_root_node, del_cmd)
        self.assertEqual('', del_res, '互信删除失败')

        text = '-----step3:执行pscp -H {nodename} {正确源文件} {目标目录};expect:失败-----'
        self.log.info(text)
        test = '-----查看主机名-----'
        self.log.info(test)
        node_name = self.common.get_sh_result(self.s_root_node1, "uname -n")
        self.assertIsNotNone(node_name, '执行失败:' + text)
        self.log.info('-----执行pscp命令-----')
        pscp_cmd = f"pscp -H {node_name} {file_path} {self.dir_path}"
        self.log.info(pscp_cmd)
        pscp_res = self.common.get_sh_result(self.primary_root_node, pscp_cmd)
        self.assertIn('error code 1', pscp_res, '执行失败')

    def tearDown(self):
        self.log.info('-----step4:清理环境;expect:成功-----')
        del_cmd = f'rm -rf {file_path}'
        self.log.info(del_cmd)
        del_res = self.common.get_sh_result(self.primary_root_node, del_cmd)
        self.assertEqual('', del_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
